package com.wmz.crm.workbench.service.impl;

import com.wmz.crm.commons.constants.Constants;
import com.wmz.crm.commons.utils.DateFormateUtil;
import com.wmz.crm.commons.utils.UUIDUtil;
import com.wmz.crm.settings.entity.User;
import com.wmz.crm.workbench.entity.*;
import com.wmz.crm.workbench.mapper.*;
import com.wmz.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("clueService")
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ContactsMapper contactsMapper;
    @Autowired
    private ClueRemarkMapper clueRemarkMapper;
    @Autowired
    private CustomerRemarkMapper customerRemarkMapper;
    @Autowired
    private ContactsRemarkMapper contactsRemarkMapper;
    @Autowired
    private ContactsActivityRelationMapper contactsActivityRelationMapper;
    @Autowired
    private ClueActivityRelationMapper clueActivityRelationMapper;
    @Autowired
    private  TranMapper tranMapper;
    @Autowired
    private TranRemarkMapper tranRemarkMapper;

    @Override
    public int saveCreateClue(Clue clue) {
        return clueMapper.insertCreateClue(clue);
    }

    @Override
    public List<Clue> queryClueByConditionForPage(Map<String,Object> map) {
        return clueMapper.selectClueByConditionForPage(map);
    }

    @Override
    public int queryCountOfClueByCondition(Map<String,Object> map) {
        return clueMapper.selectCountOfClueByCondition(map);
    }

    @Override
    public Clue queryClueById(String id) {
        return clueMapper.selectClueById(id);
    }

    @Override
    public int saveEditClueById(Clue clue) {
        return clueMapper.updateClueById(clue);
    }

    @Override
    public int deleteClueByIds(String[] ids) {
        return clueMapper.deleteClueByIds(ids);
    }

    @Override
    public Clue queryClueForDetailById(String id) {
        return clueMapper.selectClueForDetailById(id);
    }

    @Override
    public void saveConvertClue(Map<String, Object> map) {
        String clueId = (String) map.get("clueId");
        User user = (User) map.get(Constants.SESSION_USER);
        //根据id查询线索信息
        Clue clue = clueMapper.selectClueById(clueId);
        //把该线索中有关公司的信息转换到客户表中
        Customer customer = new Customer(UUIDUtil.getUUid(),clue.getOwner(),clue.getCompany(),clue.getWebsite(),
                clue.getPhone(),user.getId(), DateFormateUtil.formateDateTime(new Date()),
                clue.getContactSummary(),clue.getNextContactTime(),clue.getDescription(),clue.getAddress());
        customerMapper.insertCustomer(customer);
        //把该线索中有关个人的信息转换到联系人表中
        Contacts  contacts = new Contacts(UUIDUtil.getUUid(),clue.getOwner(),clue.getSource(),customer.getId(),clue.getFullname(),
                clue.getAppellation(),clue.getEmail(),clue.getMphone(),clue.getJob(),user.getId(),
                DateFormateUtil.formateDateTime(new Date()),clue.getDescription(),clue.getContactSummary(),clue.getNextContactTime(),
                clue.getAddress());
        contactsMapper.insertContactsFromClue(contacts);

        //根据clueId查询该线索下的所有的备注
        List<ClueRemark> remarkList = clueRemarkMapper.selectClueRemarkByClueId(clueId);
        //先判断线索备注表是否为空，不为空才进行转换
        if(remarkList!=null && remarkList.size() > 0) {
            CustomerRemark customerRemark = null;
            ContactsRemark contactsRemark = null;
            List<CustomerRemark> customerRemarkList = new ArrayList<>();
            List<ContactsRemark> contactsRemarkList = new ArrayList<>();
            //遍历线索备注表，分别拷贝给客户备注表和联系人备注表
            for(ClueRemark clueRemark:remarkList){
                //收集customerRemark
                customerRemark = new CustomerRemark(UUIDUtil.getUUid(),clueRemark.getNoteContent(),
                        clueRemark.getCreateBy(),clueRemark.getCreateTime(),clueRemark.getEditBy(),
                        clueRemark.getEditTime(),clueRemark.getEditFlag(),customer.getId());
                customerRemarkList.add(customerRemark);
                //收集contactsRemark
                contactsRemark = new ContactsRemark(UUIDUtil.getUUid(),clueRemark.getNoteContent(),
                        clueRemark.getCreateBy(),clueRemark.getCreateTime(),clueRemark.getEditBy(),
                        clueRemark.getEditTime(),clueRemark.getEditFlag(),contacts.getId());
                contactsRemarkList.add(contactsRemark);
            }
            //把该线索的备注信息转换到客户备注表中一份
            customerRemarkMapper.insertCustomerRemarkFromClueRemark(customerRemarkList);
            //把该线索的备注信息转换到联系人备注表中一份
            contactsRemarkMapper.insertContactsRemarkFromClueRemark(contactsRemarkList);
        }

        //根据clueId查询线索和市场活动的关联关系
        List<ClueActivityRelation> clueActivityRelationList = clueActivityRelationMapper.selectClueActivityRelationByClueId(clueId);
        //先判断线索市场活动关系表是否为空，不为空才进行转换
        if(clueActivityRelationList!=null && clueActivityRelationList.size()>0) {
            ContactsActivityRelation contactsActivityRelation = null;
            List<ContactsActivityRelation> contactsActivityRelationList = new ArrayList<>();
            //遍历线索市场关系表，拷贝给联系人和市场关系表
            for(ClueActivityRelation clueActivityRelation:clueActivityRelationList) {
               //封装ContactsActivityRelation
                contactsActivityRelation = new ContactsActivityRelation(UUIDUtil.getUUid(),contacts.getId(),clueActivityRelation.getActivityId());
                contactsActivityRelationList.add(contactsActivityRelation);
            }
            //把该线索和市场活动的关联关系转换到联系人和市场活动的关联关系表中
            contactsActivityRelationMapper.insertContactsActivityRelationFromClueActivityRelation(contactsActivityRelationList);
        }

        //如果需要创建交易,还要往交易表中添加一条记录
        String isCreateTran = (String) map.get("isCreateTran");
        //判断是否需要创建交易
        if("true".equals(isCreateTran)) {
            //往交易表中添加一条记录
            Tran tran = new Tran(UUIDUtil.getUUid(),user.getId(),(String) map.get("money"),(String) map.get("tradeName"),
                    (String) map.get("expectedDate"),customer.getId(),(String) map.get("stage"),null,null,
                    (String) map.get("activityId"),contacts.getId(),user.getId(),DateFormateUtil.formateDateTime(new Date()),
                    null,null,null);
            tranMapper.insertTran(tran);

            //线索的备注信息转换到交易备注表中一份
            //先判断线索备注表是否为空，不为空才进行转换
            if(remarkList!=null && remarkList.size() > 0) {
                List<TranRemark> tranRemarkList = new ArrayList<>();
                TranRemark tranRemark = null;
                //遍历线索备注表，分别拷贝给客户备注表和联系人备注表
                for(ClueRemark clueRemark:remarkList){
                    tranRemark = new TranRemark(UUIDUtil.getUUid(),clueRemark.getNoteContent(),
                            clueRemark.getCreateBy(),clueRemark.getCreateTime(),clueRemark.getEditBy(),
                            clueRemark.getEditTime(),clueRemark.getEditFlag(),tran.getId());
                    tranRemarkList.add(tranRemark);
                }
                tranRemarkMapper.insertTranRemarkFromClueRemark(tranRemarkList);
            }

            // 删除线索的备注
            clueRemarkMapper.deleteClueRemarkByClueId(clueId);
            // 删除线索和市场活动的关联关系
            clueActivityRelationMapper.deleteClueActivityRelationByClueId(clueId);
            // 删除线索
            clueMapper.deleteClueByClueId(clueId);
        }
    }
}
